package com.operator.Activitys.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;

import com.operator.Activitys.R;
import com.operator.Activitys.view.pay.listener.OnPasswordInputFinish;
import com.operator.Activitys.view.pay.widget.PopupPasswordView;


/**
 * 输入支付密码
 *
 * @author lining
 */
public abstract class PopEnterPassword extends PopupWindow {

    //支付 自定义view
    private PopupPasswordView pwdView;
    //总布局
    private View mMenuView;
    //当前 activity
    private Activity mContext;

    /**
     * 全局构造方法
     * @param context
     */
    public PopEnterPassword(final Activity context,String title,double money) {
        super(context);
        this.mContext = context;

        /**
         * 获取当前 弹窗 布局
         */
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.pop_enter_password, null);
        //获取 支付自定义View 视图
        pwdView = mMenuView.findViewById(R.id.pwd_view);


        //设置 支付信息
        pwdView.setPayMsg(title,money);

        //添加密码输入完成的响应
        pwdView.setOnFinishInput(new OnPasswordInputFinish() {
            @Override
            public void inputFinish(final String password) {
                //延时 0.3s
                new android.os.Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        //提供 给 外部实现方法
                        setOnFinishInput(password);
                        //销毁当前弹窗
                        dismiss();
                    }
                }, 300);
            }
        });

        // 监听X关闭按钮
        pwdView.getImgCancel().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        // 监听键盘上方的返回
        pwdView.getVirtualKeyboardView().getLayoutBack().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        // 设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.pop_add_animation);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x66000000);
        // 设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);

    }

    /**
     * 提供 给 外部实现方法
     */
    public abstract void setOnFinishInput(String pwd);
}
